<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
       $this->display();
    }
    public function stshow(){
        $practicesmodel=M('groups');
        $practices=$practicesmodel->query('select * from groups');
        $pm=$practicesmodel->query('select * from groups order by hit DESC ');
        $this->practices=$practices;
        $this->pm=$pm;
        $this->display();
    }

   public function stxq($id){
        $groupsModel=M('groups');
        $this->group=$groupsModel->find($id);
        $users=$groupsModel->query('update groups set hit=hit+1 where gid='.$id);
        $stuprac=$groupsModel->query('select groupname from groups where gid ='.$_GET['id']);
        $this->stxq=$stuprac;
        $this->display();
  }
  public function addmember(){
        
        $practiceModel=M('members');
        if($practiceModel->create())
         $data['gid']=$_POST['gid'];
         $data['stunum']=session('username');
         if($practiceModel->where($data)->select())
            {$this->error('已申请');}
        else {
            if ($practiceModel->add()) {
             $this->success('加入社团成功','stshow');
            }
            else{
              $this->error('加入社团失败');
                }
            }
    }
    public function addpmember(){
        
        $practiceModel=M('stuprac');
        if($practiceModel->create()){
            if ($practiceModel->add()) {
             $this->success('申请活动成功','hdshow');
            }
            else{
              $this->error('申请活动失败');
                }
            }
    }
     public function addpuser(){
        $practicesmodel=M('users');
        $practices=$practicesmodel->query('select stunum from users');
        $this->practices=$practices;
        $this->display();
    }
    public function hdshow(){
        $practicesmodel=M('practices');
        $practices=$practicesmodel->query('select * from practices');
        $pm=$practicesmodel->query('select * from practices order by hit DESC ');
        $this->practices=$practices;
        $this->pm=$pm;
        $this->display();
    }
    public function hdxq($id){
        $groupsModel=M('practices');
        $this->group=$groupsModel->find($id);
        $users=$groupsModel->query('update practices set hit=hit+1 where pid='.$id);
        $stuprac=$groupsModel->query('select pname from practices where pid ='.$_GET['id']);
        $this->stxq=$stuprac;
        $this->display();
  }
   public function dtshow(){
        $practicesmodel=M('news');
        $practices=$practicesmodel->query('select * from news');
        $pm=$practicesmodel->query('select * from news order by hit DESC ');
        $this->practices=$practices;
        $this->pm=$pm;
        $this->display();
    }

   public function dtxq($id){
        $groupsModel=M('news');
        $this->group=$groupsModel->find($id);
        $users=$groupsModel->query('update news set hit=hit+1 where nid='.$id);
        $stuprac=$groupsModel->query('select title from news where nid ='.$_GET['id']);
        $this->stxq=$stuprac;
        $this->display();
  }
function upload(){
           $upload = new \Think\Upload();// 实例化上传类
           $upload->maxSize = 3145728 ;// 设置附件上传大小
           $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
            //$upload->savePath = ''; // 设置附件上传（子）目录
            // 上传文件
            $upload->autoSub=false;
            $info = $upload->upload();
            if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
            }else{// 上传成功
            //$this->success('上传成功！');
                return $info;
            }
    }

    function img(){
        //if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
    $photoinfo=$this->upload();//将临时文件保存到指定位置
    //$userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
    //var_dump($photoinfo);
    $filePath='./Public/Uploads/photo/';
    $oldFileName=$photoinfo['photo']['savename'];
    $oldFileFullName=$filePath."\\".$oldFileName;
    $newFileName='thumb_'.$oldFileName;
    $newFileFullName=$filePath."\\".$newFileName;
    // $img=$photoinfo['photo']['rootPath'].'\\'.$photoinfo['photo']['savename'];
    $image=new \Think \Image();
    $image->open($oldFileFullName);
    $image->thumb(100,100)->save($newFileFullName);
    return $newFileName;
  }

   public function register(){
        //添加用户
        //用户名是否存在，口令是否正确，口令加密
        $userModel=D('users');
        if($userModel->create()){
            if($_FILES['photo']['error']==0)
          {
            $fileName=$this->img();
            $userModel->photo=$fileName;
          }
          //$userModel->password=md5('password');
            if ($userModel->add()) {
             $this->success('注册成功',U('Home/Index/index'));
            }
            else{
              $this->error('注册失败');
                }
            }
            else{$this->error($userModel->getError());
            }
    }

 public function  dologin(){
     $adminModel=M('users');
      $condition=array(
         'stunum'=>$_POST['stunum'],
         'password'=>$_POST['password']
         //md5(I('post.password'))
        );
      $admin=$adminModel->where($condition)->find();
      if ($admin) {
        // 是合法用户,设置session值
      session('username',$_POST['stunum']);
      $this->redirect('Index/index');
     // var_dump(session('username'));
      }
      else
      {
        // 非法用户
        $this->error('学号与密码不匹配!!!');
       // var_dump(md5(I('post.password')));
      }
  }
  public function logout(){
      session('username',null);
      //exit;
      $this->redirect('Index/index');
  } 
  public function myinfo(){
        $practicesmodel=M('users');
         $practices=$practicesmodel->query("select * from users where stunum='".session('username')."'");
        // $practices2=$practicesmodel->query("select members.* from ORDERS users  inner join members on users.stunum=members.stunum where user.username='".session('username')."'");
         $this->practices=$practices;
         //$this->practices2=$practices2;
         $groupModel=M('Groups');
         $this->group=$groupModel->join('members on groups.gid=members.gid')->where("members.stunum='".session('username')."'")->select();
         $groupModel1=M('practices');
         $this->stuprac=$groupModel1->join('stuprac on practices.pid=stuprac.pid')->where("stuprac.stunum='".session('username')."'")->select();
         $practicesmodel1=M('members');
         $practices1=$practicesmodel1->query("select * from members where stunum='".session('username')."'");
         $this->mem=$practices1;
         $this->display();
    } 
    public function tcst($id){
        //生成一个
        $usersModel=M('members');
        if($usersModel->delete($id))
        {
          $this->success('退出成功');
        }else
        {
           $this->error('退出失败');
        }
    }
    public function tchd($id){
        //生成一个
        $usersModel=M('stuprac');
        if($usersModel->delete($id))
        {
          $this->success('退出成功');
        }else
        {
           $this->error('退出失败');
        }
    }
     public function updateinfo($id){
        $usersModel=M('users');
        $this->user=$usersModel->find($id);
        $this->display();
        //将查询到的用户信息传递给模板 update.html
    }
    public function doupdate(){
        //获取updateUserInfo提交过来的数据，写入并修改数据库
    //获取参数方法 $_POST I(post.+字段名)  例$data=$_POST    $data=I(post,)
    // create()  利用$_POST数组创建一个对象  
    $userModel=D('users');
    $userModel->create();

 // if($_FILES['photo']['error']==0)//判断文件上传是否成功
  //{
   // $photoinfo=$this->upload();//将临时文件保存到指定位置
   // $userModel->photo=$photoinfo['photo']['savename'];//获取并设置文件名称
  //}
    if ($userModel->save()) {
     $this->success('修改用户成功','myinfo');
    }
    else{
      $this->error('修改用户失败');
        }
    }
    public function fzr($id){
        $practicesmodel=M('members');
        $practices=$practicesmodel->join('users on members.stunum=users.stunum')->where('members.gid='.$_GET['id'])->select();
         $this->practices=$practices;
        $groupModel=M('practices');
        $this->stuprac=$groupModel->join('groups on practices.groupname=groups.groupname')->where('groups.gid='.$_GET['id'])->select();
        // var_dump( $this->stuprac);
        $this->display();
    }
    public function delpractice($id){
        //生成一个
        $usersModel=M('practices');
        if($usersModel->delete($id))
        {
          $this->success('删除成功');
        }else
        {
           $this->error('删除失败');
        }
    }
     public function delmember($id){
        //生成一个
        $usersModel=M('members');
        if($usersModel->delete($id))
        {
          $this->success('删除成功');
        }else
        {
           $this->error('删除失败');
        }
    }
    public function setmjy($id){
        //生成一个user表对应的模型实例usersModel
         $usersModel=M('members');
        $usersModel->state=1;
        $usersModel->mid=$id;
        if ($usersModel->save()) {
            $this->success('允许参加成功');
    }else
        {
           $this->error('允许参加失败');
        }
    }
    public function delmjy($id){
        $usersModel=M('members');
        $usersModel->state=0;
        $usersModel->mid=$id;
        if ($usersModel->save()) {
            $this->success('禁止参加成功');
    }else
        {
           $this->error('禁止参加失败');
        }
    }
}